package com.dcsec.server.zebra;

import com.dcsec.server.bean.PrintParam;
import com.dcsec.server.util.ImageToZplUtil;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.printer.discovery.DiscoveredPrinterDriver;
import com.zebra.sdk.printer.discovery.DiscoveredUsbPrinter;
import com.zebra.sdk.printer.discovery.UsbDiscoverer;

import java.util.List;


/**
 * ZSDK_API 斑马官方自带连接方式
 * windows 需要dll扩展应用支持
 * @author gq
 */
public class ZebraPrinter {

    public static void main(String[] args) throws ConnectionException {

        PrintParam param = new PrintParam();
        param.setEpc("5A4320240719151100000001");
        param.setUserData("A3A832303234A3A9D4C13733C3F1D6AAB3F5303031BAC5");
        param.setImageUrl("http://127.0.0.1:8080/upload-evidence-server/export/1721617018065.png");


//        param.setUserData("EFBC8832303234EFBC89E7B2A43733E6B091E79FA5E5889D303031E58FB7");
//        param.setImageUrl("http://127.0.0.1:8080/upload-evidence-server/export/1721718010089.png");

        DiscoveredUsbPrinter zebraUsbPrinters = getZebraUsbPrinters();
        if (zebraUsbPrinters!=null) {
            Connection conn = zebraUsbPrinters.getConnection();

            try {
                //连接打印设备
                conn.open();

                ZebraPrinter.print((UsbConnection) conn, param);
            } catch (ConnectionException e) {
                throw new RuntimeException("打印失败，打印机错误："+e.getMessage());
            } finally {
                conn.close();
            }
        }
    }

    /**
     * 连接指定打印机名称 批量打印
     * @param printParams  打印数据
     */
    public static void printToParams(List<PrintParam> printParams) throws ConnectionException {

        if (printParams.isEmpty()) {
            throw new RuntimeException("打印失败：打印信息为空");
        }


        DiscoveredUsbPrinter zebraUsbPrinters = getZebraUsbPrinters();
        if (zebraUsbPrinters!=null) {
            Connection conn = zebraUsbPrinters.getConnection();
            try {
                //连接打印设备
                conn.open();

                for (PrintParam data : printParams){
                    print((UsbConnection) conn, data);
                }
            } catch (ConnectionException e) {
                throw new RuntimeException("打印失败，无法连接打印机："+e.getMessage());
            } finally {
                conn.close();
            }
        } else {
            throw new RuntimeException("打印失败：无法连接打印机");
        }
    }

    public static void print(UsbConnection conn, PrintParam param) throws ConnectionException {

        if (param==null) {
            throw new RuntimeException("打印失败：打印信息为空");
        }

        if (conn==null) {
            throw new RuntimeException("打印失败：无法连接打印机");
        }

        String imageZpl1 = ImageToZplUtil.convertFromImg(param.getImageUrl());

        String sb = "^XA"+
        //打印暗度（绝对值） 当前值30
        "~SD30"+
        //打印暗度（相对值） 当前值30 + 3
        "^MD3"+
        //打印速度2，空转速度5，回撤速度5
        "^PR2,5,5"+
        //打印反转
        "^POI"+
        //打印dpi
        "^MUd,600,600"+
        //图片Zpl
        imageZpl1+
        //写入EPC
        "^RFW,H,2,1,E^FD" + param.getEpc() + "^FS"+
        //写入USER区域数据
        "^RFW,H,0,64,3^FD" + param.getUserData() + "^FS"+
        "^XZ";

        //开始打印
        conn.write(sb.getBytes());
    }

    public static void zebraDriverPrintersTest() throws ConnectionException {
        System.out.println("Discovered USB printer list:\r\n");
        for (DiscoveredPrinterDriver printer : UsbDiscoverer.getZebraDriverPrinters()) {
            System.out.println(printer);
        }
        System.out.println("End USB printer list\r\n");

//        Connection imz = ConnectionBuilder.build("\\\\?\\usb#vid_0a5f&pid_00f2#imz220#...");
//        try {
//            imz.open();
//            byte[] hi_return = imz.sendAndWaitForResponse("~HI".getBytes(), 5000, 10000, "V");
//
//            System.out.println(new String(hi_return));
//        } finally {
//            imz.close();
//        }
    }

    /**
     * 获取USB打印设备
     */
    private static DiscoveredUsbPrinter getZebraUsbPrinters() throws ConnectionException {
        for (DiscoveredUsbPrinter printer : UsbDiscoverer.getZebraUsbPrinters()) {
            return printer;
        }
        return null;
    }
}
